﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Net;
using System.IO;
using System.Text;

namespace TireService.Web
{
    public partial class CSV_test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (WebClient client = new WebClient())
            {
                //string file = client.DownloadString(@"http://viggolaursen.dk/export/prisfil.asp?debitornr=81613613&password=MOBIL");

                string appdatafolder = Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, "App_Data");

                //string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

                //// Create the file.
                //using (FileStream fs = File.Create(appdatafolder + "\\Test.csv", 1024))
                //{
                //    Byte[] info = new UTF8Encoding(true).GetBytes(file);
                //    // Add some information to the file.
                //    fs.Write(info, 0, info.Length);
                //}


                //var jobs = File.ReadLines(appdatafolder + "\\Test.csv").Select(line => line.Split(';')).Select(tokens => new { Field0 = tokens[0], Field1 = tokens[1] }).ToList();

                var csvData =
                    from row in File.ReadLines(appdatafolder + "\\Test.csv")
                    let columns = row.Split(';')
                    select new
                    {
                        ID = columns[0],
                        Name = columns[1],
                        Email = columns[2]
                    };





                //var stuff = from l in File.ReadAllLines(file)
                //            let x = l.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries)
                //                     .Skip(1)
                //                     .Select(s => Convert.ToString(s))
                //            select x;

                ASPxGridView1.DataSource = csvData.Take(10).ToList();
                ASPxGridView1.DataBind();
            }

        }
    }
}